{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 9.3 \u89e3\u9664\u4e00\u4e2a\u88c5\u9970\u5668\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u4e2a\u88c5\u9970\u5668\u5df2\u7ecf\u4f5c\u7528\u5728\u4e00\u4e2a\u51fd\u6570\u4e0a\uff0c\u4f60\u60f3\u64a4\u9500\u5b83\uff0c\u76f4\u63a5\u8bbf\u95ee\u539f\u59cb\u7684\u672a\u5305\u88c5\u7684\u90a3\u4e2a\u51fd\u6570\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5047\u8bbe\u88c5\u9970\u5668\u662f\u901a\u8fc7 @wraps (\u53c2\u80039.2\u5c0f\u8282)\u6765\u5b9e\u73b0\u7684\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee __wrapped__ \u5c5e\u6027\u6765\u8bbf\u95ee\u539f\u59cb\u51fd\u6570\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "@somedecorator\ndef add(x, y):\n    return x + y\norig_add = add.__wrapped__\norig_add(3, 4)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u76f4\u63a5\u8bbf\u95ee\u672a\u5305\u88c5\u7684\u539f\u59cb\u51fd\u6570\u5728\u8c03\u8bd5\u3001\u5185\u7701\u548c\u5176\u4ed6\u51fd\u6570\u64cd\u4f5c\u65f6\u662f\u5f88\u6709\u7528\u7684\u3002\n\u4f46\u662f\u6211\u4eec\u8fd9\u91cc\u7684\u65b9\u6848\u4ec5\u4ec5\u9002\u7528\u4e8e\u5728\u5305\u88c5\u5668\u4e2d\u6b63\u786e\u4f7f\u7528\u4e86 @wraps \u6216\u8005\u76f4\u63a5\u8bbe\u7f6e\u4e86 __wrapped__ \u5c5e\u6027\u7684\u60c5\u51b5\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u6709\u591a\u4e2a\u5305\u88c5\u5668\uff0c\u90a3\u4e48\u8bbf\u95ee __wrapped__ \u5c5e\u6027\u7684\u884c\u4e3a\u662f\u4e0d\u53ef\u9884\u77e5\u7684\uff0c\u5e94\u8be5\u907f\u514d\u8fd9\u6837\u505a\u3002\n\u5728Python3.3\u4e2d\uff0c\u5b83\u4f1a\u7565\u8fc7\u6240\u6709\u7684\u5305\u88c5\u5c42\uff0c\u6bd4\u5982\uff0c\u5047\u5982\u4f60\u6709\u5982\u4e0b\u7684\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from functools import wraps\n\ndef decorator1(func):\n    @wraps(func)\n    def wrapper(*args, **kwargs):\n        print('Decorator 1')\n        return func(*args, **kwargs)\n    return wrapper\n\ndef decorator2(func):\n    @wraps(func)\n    def wrapper(*args, **kwargs):\n        print('Decorator 2')\n        return func(*args, **kwargs)\n    return wrapper\n\n@decorator1\n@decorator2\ndef add(x, y):\n    return x + y"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u6211\u4eec\u5728Python3.3\u4e0b\u6d4b\u8bd5\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "add(2, 3)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "add.__wrapped__(2, 3)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u6211\u4eec\u5728Python3.4\u4e0b\u6d4b\u8bd5\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "add(2, 3)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "add.__wrapped__(2, 3)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u8981\u8bf4\u7684\u662f\uff0c\u5e76\u4e0d\u662f\u6240\u6709\u7684\u88c5\u9970\u5668\u90fd\u4f7f\u7528\u4e86 @wraps \uff0c\u56e0\u6b64\u8fd9\u91cc\u7684\u65b9\u6848\u5e76\u4e0d\u5168\u90e8\u9002\u7528\u3002\n\u7279\u522b\u7684\uff0c\u5185\u7f6e\u7684\u88c5\u9970\u5668 @staticmethod \u548c @classmethod \u5c31\u6ca1\u6709\u9075\u5faa\u8fd9\u4e2a\u7ea6\u5b9a\n(\u5b83\u4eec\u628a\u539f\u59cb\u51fd\u6570\u5b58\u50a8\u5728\u5c5e\u6027 __func__ \u4e2d)\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}